﻿using Tele.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tele.db.IDataAdapters.Interfaces;

namespace Tele.db.IDataAdapters.Adapters
{
    public class TVShowAdapter : ITVShowAdapter
    {
        public TVShow AddTVShow(TVShow tvshow)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                tvshow = db.TVShows.Add(tvshow);
                db.SaveChanges();
            }
            return tvshow;
        }

        public List<TVShow> GetAllTVShows()
        {
            List<TVShow> tvshows;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                tvshows = db.TVShows.OrderBy(x => x.Title).ToList();
            }
            return tvshows;
        }

        public TVShow GetTVShow(int id)
        {
            TVShow tvshow;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                tvshow = db.TVShows.Include("Characters").Where(x => x.Id == id).FirstOrDefault();
            }
            return tvshow;
        }

        public TVShow UpdateTVShow(TVShow tvshow)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                TVShow oldtvshow = db.TVShows.Find(tvshow.Id);
                oldtvshow.setProps(tvshow);
                db.SaveChanges();
            }
            return tvshow;
        }

        public void DeleteTVShow(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                TVShow tvshow = db.TVShows.Find(id);
                db.TVShows.Remove(tvshow);
                db.SaveChanges();
            }
        }

        /********************* Character CRUD *******************************/
        public Character AddCharacter(Character character)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.Characters.Add(character);
                db.SaveChanges();
            }
            return character;
        }

        public List<Character> GetAllCharacters()
        {
            List<Character> characters;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                characters = db.Characters.OrderBy(x => x.Name).ToList();
            }
            return characters;
        }

        public Character GetCharacter(int id)
        {
            Character character;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.Characters.Include("TVShows").Where(x => x.Id == id).FirstOrDefault();
            }
            return character;
        }

        public Character UpdateCharacter(Character character)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Character oldcharacter = db.Characters.Find(character.Id);
                oldcharacter.setProps(character);
                db.SaveChanges();
            }
            return character;
        }

        public void DeleteCharacter(int id)
        {
            throw new NotImplementedException();
        }
    }
}
